import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class ContactClient extends JFrame {
    private ContactService contactService;
    private JTextArea contactTextArea;

    public ContactClient() {
        contactService = new ContactService();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("个人通讯录");
        setSize(500, 400);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        contactTextArea = new JTextArea();
        contactTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(contactTextArea);
        panel.add(scrollPane, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        JButton addButton = new JButton("添加联系人");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addContact();
            }
        });
        buttonPanel.add(addButton);

        JButton deleteButton = new JButton("删除联系人");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteContact();
            }
        });
        buttonPanel.add(deleteButton);

        JButton updateButton = new JButton("修改联系人");
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateContact();
            }
        });
        buttonPanel.add(updateButton);

        panel.add(buttonPanel, BorderLayout.SOUTH);



        add(panel);
    }

    public void showContacts() {
        ArrayList<Contact> contacts = contactService.queryContact();
        StringBuilder sb = new StringBuilder();
        for (Contact contact : contacts) {
            sb.append("姓名：").append(contact.getName()).append("\n");
            sb.append("地址：").append(contact.getAddress()).append("\n");
            sb.append("电话：").append(contact.getPhone()).append("\n\n");
        }
        contactTextArea.setText(sb.toString());
    }

    public void addContact() {
        String name = JOptionPane.showInputDialog(this, "请输入联系人姓名：");
        String address = JOptionPane.showInputDialog(this, "请输入联系人地址：");
        String phone = JOptionPane.showInputDialog(this, "请输入联系人电话：");

        Contact contact = new Contact(name, address, phone);
        boolean success = contactService.addContact(contact);

        if (success) {
            JOptionPane.showMessageDialog(this, "联系人添加成功！");
        } else {
            JOptionPane.showMessageDialog(this, "联系人添加失败！");
        }

        showContacts();
    }

    public void deleteContact() {
        String name = JOptionPane.showInputDialog(this, "请输入要删除的联系人姓名：");

        boolean success = contactService.deleteContact(name);

        if (success) {
            JOptionPane.showMessageDialog(this, "联系人删除成功！");
        } else {
            JOptionPane.showMessageDialog(this, "联系人删除失败！");
        }

        showContacts();
    }

    public void updateContact() {
        String name = JOptionPane.showInputDialog(this, "请输入要修改的联系人姓名：");
        String newName = JOptionPane.showInputDialog(this, "请输入新的联系人姓名：");
        String newAddress = JOptionPane.showInputDialog(this, "请输入新的联系人地址：");
        String newPhone = JOptionPane.showInputDialog(this, "请输入新的联系人电话：");

        Contact contact = new Contact(newName, newAddress, newPhone);
        boolean success = contactService.updateContact(name,contact);

        if (success) {
            JOptionPane.showMessageDialog(this, "联系人修改成功！");
        } else {
            JOptionPane.showMessageDialog(this, "联系人修改失败！");
        }

        showContacts();
    }

    public static void main(String[] args) {
        ContactClient client = new ContactClient();
        client.setVisible(true);
        client.showContacts();
    }
}
